<%@ page import="com.chazz.service.impl.CartServiceImpl" %>
<%@ page import="com.chazz.entity.CartDetails" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %><%--
  Created by IntelliJ IDEA.
  User: Dell
  Date: 2020/9/23
  Time: 9:34
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    CartServiceImpl cartService = new CartServiceImpl();
//    Cookie[] cookie = request.getCookies();
//    for(int i = 0;i<cookie.length;i++){
//
//    }
    List<CartDetails> cartDetailsList = cartService.findDetailsByGid();
    request.setAttribute("cartDetailsList", cartDetailsList);
%>
<html>
<head>
    <meta charset="utf-8">
    <title>Cart</title>

    <link rel="stylesheet" type="text/css" href="css/reset.css"/>
    <link rel="stylesheet" type="text/css" href="css/jd.css"/>

    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="keywords" content=""/>

    <!-- Custom Theme files -->
    <link href="css/bootstrap.css" rel="stylesheet" type="text/css" media="all" />
    <link href="css/style.css" rel="stylesheet" type="text/css" media="all" />
    <link href="css/menu.css" rel="stylesheet" type="text/css" media="all" /> <!-- menu style -->
    <link href="css/ken-burns.css" rel="stylesheet" type="text/css" media="all" /> <!-- banner slider -->
    <link href="css/animate.min.css" rel="stylesheet" type="text/css" media="all" />
    <link href="css/owl.carousel.css" rel="stylesheet" type="text/css" media="all"> <!-- carousel slider -->
    <!-- //Custom Theme files -->
    <!-- font-awesome icons -->
    <link href="css/font-awesome.css" rel="stylesheet">
    <!-- //font-awesome icons -->
    <!-- js -->
    <!-- //js -->
    <!-- web-fonts -->
    <link href='http://fonts.useso.com/css?family=Roboto+Condensed:400,300,300italic,400italic,700,700italic' rel='stylesheet' type='text/css'>
    <link href='http://fonts.useso.com/css?family=Lovers+Quarrel' rel='stylesheet' type='text/css'>
    <link href='http://fonts.useso.com/css?family=Offside' rel='stylesheet' type='text/css'>
    <link href='http://fonts.useso.com/css?family=Tangerine:400,700' rel='stylesheet' type='text/css'>
    <!-- web-fonts -->

    <script src="js/jquery-3.3.1.js"/>
    <script src="js/owl.carousel.js"></script>
<%--    <script>--%>
<%--        $(document).ready(function() {--%>
<%--            $("#owl-demo").owlCarousel({--%>
<%--                autoPlay: 3000, //Set AutoPlay to 3 seconds--%>
<%--                items :4,--%>
<%--                itemsDesktop : [640,5],--%>
<%--                itemsDesktopSmall : [480,2],--%>
<%--                navigation : true--%>

<%--            });--%>
<%--        });--%>
<%--    </script>--%>
    <script src="js/jquery-scrolltofixed-min.js" type="text/javascript"></script>
    <script>
        $(document).ready(function() {

            // Dock the header to the top of the window when scrolled past the banner. This is the default behaviour.

            $('.header-two').scrollToFixed();
            // previous summary up the page.

            var summaries = $('.summary');
            summaries.each(function(i) {
                var summary = $(summaries[i]);
                var next = summaries[i + 1];

                summary.scrollToFixed({
                    marginTop: $('.header-two').outerHeight(true) + 10,
                    zIndex: 999
                });
            });
        });
    </script>
    <!-- start-smooth-scrolling -->
    <script type="text/javascript" src="js/move-top.js"></script>
    <script type="text/javascript" src="js/easing.js"></script>
    <script type="text/javascript"></script>
    <!-- //end-smooth-scrolling -->
    <!-- smooth-scrolling-of-move-up -->
    <script type="text/javascript">
        $(document).ready(function() {

            var defaults = {
                containerID: 'toTop', // fading element id
                containerHoverID: 'toTopHover', // fading element hover id
                scrollSpeed: 1200,
                easingType: 'linear'
            };

            $().UItoTop({ easingType: 'easeOutQuart' });

        });
    </script>
    <!-- //smooth-scrolling-of-move-up -->
    <script src="js/bootstrap.js"></script>
</head>
<body>

<!--导航开始-->


<script>
    // $('#myModal88').modal('show');
</script>
<!-- header -->
<div class="header">
    <div class="w3ls-header"><!--header-one-->
        <div class="w3ls-header-left">
            <p><a href="#">UPTO $50 OFF ON LAPTOPS | USE COUPON CODE LAPPY </a></p>
        </div>
        <div class="w3ls-header-right">
            <ul>
                <li class="dropdown head-dpdn">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-user" aria-hidden="true"></i> My Account<span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="login.html">Login </a></li>
                        <li><a href="signup.html">Sign Up</a></li>
                        <li><a href="login.html">My Orders</a></li>
                        <li><a href="login.html">Wallet</a></li>
                    </ul>
                </li>
                <li class="dropdown head-dpdn">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-percent" aria-hidden="true"></i> Today's Deals<span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="offers.html">Cash Back Offers</a></li>
                        <li><a href="offers.html">Product Discounts</a></li>
                        <li><a href="offers.html">Special Offers</a></li>
                    </ul>
                </li>
                <li class="dropdown head-dpdn">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-gift" aria-hidden="true"></i> Gift Cards<span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="offers.html">Product Gift card</a></li>
                        <li><a href="offers.html">Occasions Register</a></li>
                        <li><a href="offers.html">View Balance</a></li>
                    </ul>
                </li>
                <li class="dropdown head-dpdn">
                    <a href="contact.html" class="dropdown-toggle"><i class="fa fa-map-marker" aria-hidden="true"></i> Store Finder</a>
                </li>
                <li class="dropdown head-dpdn">
                    <a href="card.html" class="dropdown-toggle"><i class="fa fa-credit-card-alt" aria-hidden="true"></i> Credit Card</a>
                </li>
                <li class="dropdown head-dpdn">
                    <a href="help.html" class="dropdown-toggle"><i class="fa fa-question-circle" aria-hidden="true"></i> Help</a>
                </li>
            </ul>
        </div>
        <div class="clearfix"> </div>
    </div>
</div>
<!--导航结束-->



<!--标题开始-->
<div class="title warp">

    <h3>全部商品</h3>
</div>

<!--标题结束-->

<!--显示菜单的开始-->
<div class="tips warp">

    <ul>
        <li>商品</li>
        <li>标题</li>
        <li style="margin-left: 186px;">描述</li>
        <li style="margin-left: 89px;">单价</li>
        <li style="margin-left: 71px;">数量</li>
        <li style="margin-left: 86px;">小计</li>
        <li style="margin-left: 58px;">操作</li>
    </ul>
</div>

<!--商品详情展示开始-->

<c:if test="${not empty user}">
    <c:if test="${not empty cartDetails}">
        <c:forEach items="${cartDetails}" var="cartDetail">
            <div class="info warp">
                <ul>
                    <li class="info_1"><input class="choice" type="checkbox" name="choice" value="${cartDetail.good.goodsid}" /> </li>
                    <li class="info_2"> <img src="${cartDetail.good.goodsimg}" width="80px"/> </li>
                    <li class="info_3"><a>${cartDetail.good.goodstitle}</a></li>
                    <li class="info_4">${cartDetail.good.goodsdescription}</li>
                    <li class="info_5">${cartDetail.price}</li>
                    <li class="info_6" value="${user.id}">
                        <button onclick="javascrtpt:window.location.href='./cart.do?method=deleteOne&cid=${cartDetail.cart.id}&gid=${cartDetail.good.goodsid}'">-</button>
                        <input type="text" name="count" class="count" value="${cartDetail.count}" />
                        <button class="bot" onclick="javascrtpt:window.location.href='./cart.do?method=add&gid=${cartDetail.good.goodsid}'">+</button>
                    </li>
                    <li class="info_7">${cartDetail.count*cartDetail.price}</li>
                    <li>
                        <a href="./cart.do?method=delete&gid=${cartDetail.good.goodsid}">删除</a>
                    </li>
                </ul>
            </div>
        </c:forEach>
    </c:if>
</c:if>
<c:if test="${empty uaer}">
    <c:if test="${not empty cartDetailsList}">
        <c:forEach items="${cartDetailsList}" var="cookiecartDetail">
            <div class="info warp">
                <ul>
                    <li class="info_1"><input class="choice" type="checkbox" name="choice" value="${cookiecartDetail.good.goodsid}" /> </li>
                    <li class="info_2"> <img src="${cookiecartDetail.good.goodsimg}" width="80px"/> </li>
                    <li class="info_3"><a>${cookiecartDetail.good.goodstitle}</a></li>
                    <li class="info_4">${cookiecartDetail.good.goodsdescription}</li>
                    <li class="info_5">${cookiecartDetail.price}</li>
                    <li class="info_6" value="${user.id}">
                        <button onclick="javascrtpt:window.location.href='./cart.do?method=deleteOne&gid=${cookiecartDetail.good.goodsid}&cid=${cookiecartDetail.cart.id}'">-</button>
                        <input type="text" name="count" class="count" value="${cookiecartDetail.count}" />
                        <button class="bot" onclick="javascrtpt:window.location.href='./cart.do?method=add&gid=${cookiecartDetail.good.goodsid}'">+</button>
                    </li>
                    <li class="info_7">${cookiecartDetail.count*cookiecartDetail.price}</li>
                    <li>
                        <a href="./cart.do?method=delete&gid=${cookiecartDetail.good.goodsid}">删除</a>
                    </li>
                </ul>
            </div>
        </c:forEach>
    </c:if>
</c:if>

<!--商品详情展示结束-->

<!--结算开始-->
<div class="balance warp">

    <ul class="balance_ul1">
        <li>
            <input class="allCheck" id="allCheck" type="checkbox" name="allCheck" value="" />
            全选
        </li>
        <li><a href="./cart.do?method=deleteAll">全部删除</a></li>
    </ul>

    <ul class="balance_ul2">
<%--        readonly disabled style="border:none;"--%>
        <li>已经选择<input type="text" class="cous" value="0" readonly disabled style="border:none;hight:30px;width: 30px">件商品</li>
        <li>总价 <input type="text" class="total" value="0" readonly disabled style="border:none;hight:30px;width: 60px"></li>
        <li>
            <button class="butt">去结算</button>

        </li>

    </ul>


</div>
<!--结算结束-->
<script>
    $(document).ready(function () {
        //登录时购物车
        //全选
        $('input[name="allCheck"]').on("click",function(){
            if($(this).is(':checked')){
                $('input[name="choice"]').each(function(){
                    $(this).prop("checked",true);
                });
            }else{
                $('input[name="choice"]').each(function(){
                    $(this).prop("checked",false);
                });
            }
        });
        //单选
        $('input[name="choice').on("click",function () {
            if($(this).not(':checked')){
                $('input[name="allCheck"]').prop("checked",false);
            }
        })

        var a = {}
        var clicks = document.getElementsByName("choice");
        var discount = [];
        var c=0;
        //获取商品id和数量
        $(".choice").click(function () {
            $(".total").val(0);
            c = 0;
            discount.splice(0);
            $(".cous").val(0);
            var i = 0;
                $("input:checkbox[name='choice']:checked").each(function(){
                    var a = {};
                    a.id = $(".info_6").val();
                    a.count = $(".count").eq(i).val();
                    a.goodsId = $(this).val();
                    c = c + Number($(".count").eq(i).val());
                    i++;
                    discount.push(a);
                    $(".cous").val(c);
                });
                if($(".info_6").val()==0){
                    $.ajax({
                        "url":"cart.do",
                        "type":"POST",
                        "data":{
                            "goods":JSON.stringify(discount),
                            "method":"list"
                        },
                        "datatype":"JSON",
                        "success":function (result) {
                            $(".total").val(result);
                        }
                    })
                }else{
                    $.ajax({
                    "url":"discount.do",
                    "type":"POST",
                    "data":{
                        "goods":JSON.stringify(discount),
                        "method":"list"
                    },
                    "datatype":"JSON",
                    "success":function (result) {
                        $(".total").val(result);
                    }
                    })
                }

        })
        //全选获取值
        $(".allCheck").click(function () {
            $(".total").val(0);
            discount.splice(0);
            c = 0;
            $(".cous").val(0);
            if($(this).is(':checked')){
                var i = 0;
                $("input:checkbox[name='choice']:checked").each(function(){
                    var a = {};
                    a.id = $(".info_6").val();
                    a.count = $(".count").eq(i).val();
                    a.goodsId = $(this).val();
                    c = c + Number($(".count").eq(i).val())
                    $(".cous").val(c);
                    discount.push(a);
                });
            }
            $.ajax({
                "url":"discount.do",
                "type":"POST",
                "data":{
                    "goods":JSON.stringify(discount),
                    "method":"list"
                },
                "datatype":"JSON",
                "success":function (result) {
                    $(".total").val(result);
                }
            })
        })


        //结算时生产订单表，并将订单传到ordersave.do中储存到数据库
        var orders = [];
        var info = [];
        $(".butt").click(function () {
            info.splice(0);
            var i=0
            $("input:checkbox[name='choice']:checked").each(function(){
                var a = {};
                a.goodsid = $(this).val();
                a.userid = $(".info_6").val();
                a.count = $(".count").eq(i).val();
                i++;
                info.push(a);
            });
            var order = {};
            order.id = 0;
            order.uid = $(".info_6").val();
            order.tatolprice = $(".total").val();
            order.state = 0;
            order.isdelete = 1;
            order.createtime = null;
            order.orderadress = null;
            orders.push(order);
            $.ajax({
                "url":"ordersave.do",
                "type":"POST",
                "data":{
                    "orders":JSON.stringify(orders),
                    "goods":JSON.stringify(discount)
                },
                "datatype":"JSON",
                "success":function (result) {
                    alert("sdsad");
                   window.open("/foraging/order.html")
                }
            })
        })





    });
</script>
</body>


</html>
